<?php
/**
 * @desc 业务员走访信息
 * @author kevin wang
 * @date 2014-09-20
 */
class ctrl_custormvisit extends ctrl_base {
    private static $dirName = 'custormvisit';

	private static $_visit_log_attr = array('序号','走访客户','客户归属单位','客户归属地址','客户维护人','接待人姓名','接待人职务','接待人电话','接待人邮箱','用户概况','合作展望','可见微信ID','走访业务员','走访日期','合作状态','审核状态','审核结论','审核日期','业务员归属单位');
    /**
     * @desc 信息列表
	 * @author kevin wang
	 * @param void
	 * @return void
     */
    public static function main() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('cvisit_list'));
		//页面输入参数处理
    	$currentPage = isset($_POST['pageNum']) ? $_POST['pageNum'] : 1;
    	$pagesize = isset($_POST['numPerPage']) ? $_POST['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

    	$visit_custorm = isset($_POST['visit_custorm']) ? trim($_POST['visit_custorm']) : '';
    	$visiter_name = isset($_POST['visiter_name'])? trim($_POST['visiter_name']):'';
    	//搜索条件组合
		$condition = array();
    	if(!empty($visit_custorm)) {
    		$condition['visit_custorm'] = $visit_custorm;
    	}
		if(!empty($visiter_name)) {
    		$condition['visiter_name'] = $visiter_name;
    	}
		//列表获取
    	$cvisit_list = model_custormvisit :: custormvisitList($nextpage,$pagesize,$condition);
    	$total = array_pop($cvisit_list);
		$unit_category = model_custormvisit::get_unit_category();
		$unit_category = self::createList($unit_category);
		self::smarty() -> assign('unit_category', $unit_category);
		//模板赋值
    	self::smarty() -> assign('cvisit_list', $cvisit_list);
		self::smarty() -> assign('total', $total);
		self::smarty() -> assign('pageNum', $currentPage);
		self::smarty() -> assign('numPerPage', $pagesize);
		self::smarty() -> assign('pageNumShown', self::$_pageNumShown);
		self::smarty() -> assign('visit_custorm', $visit_custorm);
		self::smarty() -> assign('visiter_name', $visiter_name);

		self::smarty() -> assign('shoNum', self::$_shoNum);
		
    	self::smarty() -> display(self::$dirName . '/custormvisit_list.html');    	
    }
	//添加走访信息
	public static function custormvisitAdd(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('cvisit_add'));
		if($_POST){

			$cvisit = self::get_request_data();
			unset($cvisit['parent_id']);
			unset($cvisit['category_1']);
			unset($cvisit['category_2']);
			$cvisit['visible'] = self::handle_category();
			$cvisit['author'] = $_SESSION['admin']['username'];
			$cvisit['editer'] = $_SESSION['admin']['username'];
			if(!empty($cvisit['openids'])){
				self::limit_weixin_id($cvisit['openids']);
			}

			$result = model_custormvisit::custormvisitAdd($cvisit);
			if(!empty($result)){
				self::ajax_ouput(200, '操作成功','custorm_visit','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			}
		}
		$category = model_news::get_user_category();
		self::smarty() -> assign('category', $category);
		$unit_category = model_custormvisit::get_unit_category();
		self::smarty() -> assign('unit_category', $unit_category);
		self::smarty() -> display(self::$dirName . '/custormvisit_add.html');
	}
	//删除走访信息
	public static function custormvisitDelete() {
    	if(!model_admin::islogin()) {
			self::goto_login();
    	}
		self::operation(array('cvisit_delete'));
    	$ids = $_POST['ids'];
		$ids = explode(",", $ids);
		foreach($ids as $v){
			$id[] = intval($v);
		}
    	$data = array('delete_flag' => 1);

    	$st = model_custormvisit::updateCustormvisit($id, $data);

    	if($st > 0) {
			self::ajax_ouput(200, '操作成功','custorm_visit');
    	} else {
			self::ajax_ouput(300, '操作失败');
    	}
    }
	//编辑走访信息
	public static function custormvisitEdit() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = isset($_GET['id'])? (intval($_GET['id'])):(intval($_POST['id']));
    	if($id <= 0) {
			self::ajax_ouput(300, '非法访问');
    	}
		$cvisit = model_custormvisit::getCustormvisitById($id);
		$ctrl_tag = 'cvisit_edit_other';
		if($order['author'] == $_SESSION['admin']['username']){
			$ctrl_tag = 'cvisit_edit_self';
		}
		self::operation(array($ctrl_tag));
    	if($_POST){
			$cvisit = array();

			$cvisit = self::get_request_data();
			$cvisit['editer'] = $_SESSION['admin']['username'];
			unset($cvisit['parent_id']);
			unset($cvisit['category_1']);
			unset($cvisit['category_2']);
			unset($cvisit['id']);
			$cvisit['visible'] = self::handle_category();
			if(!empty($cvisit['openids'])){
				self::limit_weixin_id($cvisit['openids']);
			}

			$result =  model_custormvisit::updateCustormvisit(explode(',',$id), $cvisit);
	    	
		    if($result !== false){
				self::ajax_ouput(200, '操作成功','custorm_visit','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			}    		
    	}
		if($cvisit['visible'] == '*_*'){
			$cvisit['visible'] = '1_8,1_9,1_10,1_11,1_12,1_13,2_3,2_4,2_5,2_6,2_7';
		}
		$visible = explode(",", $cvisit['visible']);//model_news::get_user_type_of_news($id);
		$cvisit['visible'] = array();
		$cvisit['visibleparents'] = array();
		if(!empty($visible)){
			foreach($visible as $v){
				$arr = explode("_", $v);
				if(!empty($arr[1])){
					$cvisit['visible'][] = $arr[1];//$v['category_id'];
				}
				if(!in_array($arr[0],$info['visibleparents'])){
					$cvisit['visibleparents'][] = $arr[0];
				}
			}
		}
		if(empty($cvisit['visible'])){
			$cvisit['visible'] = "*_*";
		}
		$category = model_news::get_user_category();
		self::smarty() -> assign('category', $category);
		$unit_category = model_custormvisit::get_unit_category();
		self::smarty() -> assign('unit_category', $unit_category);
    	self::smarty() -> assign('id', $id);
		if($cvisit['handle_date'] == '0000-00-00 00:00:00'){
			$cvisit['handle_date'] = '';
		}
		if($cvisit['visiter_date'] == '0000-00-00 00:00:00'){
			$cvisit['visiter_date'] = '';
		}
		if(!empty($cvisit['image'])){
			$arr = explode('/', $cvisit['image']);
			$cvisit['image_name'] = $arr[count($arr) - 1];
		}else{
			$cvisit['image_name'] = '暂无照片';
		}
    	self::smarty() -> assign('cvisit', $cvisit);
    	self::smarty() -> display(self::$dirName . '/custormvisit_edit.html');
    }
	//归属列表
	public static function createList($data){
		$return = array();
		if(empty($data)){
			return $return;
		}
		foreach($data as $value){
			$return[$value['unit_category_id']] = $value['category_name'];
		}
		return $return;
	}
	//批量上传模板下载
	public static function get_betch_model(){
		self::download_xls_output(PATH_WWW.DOWNLOAD_FILES_PATH.'betch_add_cvisit'.self::$_excel_pre);
	}
	//批量上传走访信息
	public static function batch(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('cvisit_add'));
		//文件上传
		$info = self::file_upload_xls();
		//xls文件解析
		framework_base_autoloader::import_vendor('excel/PHPExcel.php');
		$objPHPExcel = new PHPExcel();
		$objPHPExcel = PHPExcel_IOFactory::load($info['url']);
		$csivit = $objPHPExcel->getSheet(0)->toArray();
		//数据读取
		unset($csivit[0]);
		foreach($csivit as $key => $value){
			$len = count($value);
			$csivit[$key][$len] = $_SESSION['admin']['username'];
			$csivit[$key][$len + 1] = $_SESSION['admin']['username'];
		}
		$result = model_custormvisit::batch_insert_cvisit($csivit);
		if(!empty($result)){
			self::ajax_ouput(200, '操作成功','custorm_visit','closeCurrent');
		}else{
			self::ajax_ouput(300, '操作失败,请重新上传');
		}
	}
	//图片查看
	public static function view_image(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = intval($_GET['id']);
		$cvisit = model_custormvisit::getCustormvisitById($id);
		self::smarty() -> assign('cvisit', $cvisit);
    	self::smarty() -> display(self::$dirName . '/view_image.html');
	}
	//导出走访记录
	public static function custormvisitExport(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('cvisit_export'));
    	//页面输入参数处理
    	$currentPage = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1;
    	$pagesize = isset($_GET['numPerPage']) ? $_GET['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

    	$visiter_name = isset($_GET['visiter_name'])? trim($_GET['visiter_name']):'';
    	$visit_custorm = isset($_GET['visit_custorm'])? trim($_GET['visit_custorm']):'';
    	//搜索条件组合
		$condition = array();
		if(!empty($visiter_name)) {
    		$condition['visiter_name'] = $visiter_name;
    	}
		if(!empty($visit_custorm)) {
    		$condition['visit_custorm'] = $visit_custorm;
    	}
		//订单获取
    	$visit_list = model_custormvisit :: custormvisitList($nextpage,$pagesize,$condition);
    	$total = array_pop($visit_list);
		array_unshift($visit_list,self::$_visit_log_attr);
		$unit_category = model_custormvisit::get_unit_category();
		$unit_category = self::createList($unit_category);
		//var_dump($visit_list);exit;
		//载入xls处理类
		$time = time();
		$title = "业务员走访信息";
		$subject = "visit log-{$_SESSION['admin']['username']}-{$time}";
		$descript = "visit log-{$_SESSION['admin']['username']}-{$time}";
		$username = $_SESSION['admin']['username'];
		framework_base_autoloader::import_vendor('excel/PHPExcel.php');
		$objPHPExcel = new PHPExcel();
		//设置属性
		$objPHPExcel->getProperties()->setCreator($username);
		$objPHPExcel->getProperties()->setLastModifiedBy($username);
		$objPHPExcel->getProperties()->setTitle($title);
		$objPHPExcel->getProperties()->setSubject($subject);
		$objPHPExcel->getProperties()->setDescription($descript);
		//添加数据
		$objPHPExcel->setActiveSheetIndex(0);
		$cellFirst = 65;
		foreach($visit_list as $keys => $values){
			$i = 0;
			foreach($values as $k=>$v){
				//if($k == 'visit_log_id')continue;
				$parentKey = chr($cellFirst + $i);
				$cellKey = $parentKey.($keys+1);
				$cellVlue = $visit_list[$keys][$k];
				if($keys != 0 && $k == 'cooperation_status'){
					$cellVlue = ($cellVlue == 1)?'合作中':($v == 2?'合作中断':'无合作历史');
				}
				if($keys != 0 && $k == 'status'){
					$cellVlue = ($cellVlue == 0)?'审核中':'已审核';
				}
				if(($keys != 0 && $k == 'business_unit') || ($keys != 0 && $k == 'custorm_unit')){
					$cellVlue = $unit_category[$cellVlue];
				}
				$objPHPExcel->getActiveSheet()->SetCellValue($cellKey, $cellVlue);
				$i++;
			}
		}
		// Rename sheet
		$objPHPExcel->getActiveSheet()->setTitle($title);
		switch(self::$_excel_pre){
			case '.xls':$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);break;
			case '.xlsx':$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);break;//Save Excel 2007 file
			default:$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);break;
		}		 
		$down = self::get_down_file_url(self::$_excel_pre);
		$objWriter->save($down['save']);
		self::download_xls_output($down['save'], $down['down']); 
	}
}
